﻿using System;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
    public class LoginDAO//:BaseDAO<RELAXEntities,RelaxLogin>
    {
        #region value
        private readonly RelaxDataContext _context = new RelaxDataContext();
        #endregion

        #region Hàm Lấy dữ liệu

        public IQueryable<RelaxLoginsDataContract> GetAllUser()
        {
            var query = from l in _context.RelaxLogins
                select new RelaxLoginsDataContract
                {
                    Id = Convert.ToInt32(l.id),
                    Active =Convert.ToInt32(l.active),
                    MemberId = Convert.ToInt32(l.memberid??0),
                    UserName = l.username,
                    Password = l.password,
                    StaffId = Convert.ToInt32(l.Staffid??0),
                    Role = Convert.ToInt32(l.role??0)
                };
            return query;
        }
        public IQueryable<RelaxLoginsDataContract> GetAllClientUser()
        {
            var query = from l in _context.RelaxLogins
                join m in _context.RelaxMembers on l.memberid equals m.id
                        select new RelaxLoginsDataContract
                        {
                            Id = Convert.ToInt32(l.id),
                            Active = Convert.ToInt32(l.active),
                            MemberId = Convert.ToInt32(l.memberid ?? 0),
                            UserName = l.username,
                            Password = l.password,
                            Image = m.image,
                            Role = Convert.ToInt32(l.role ?? 0)
                        };
            return query;
        }

        public IQueryable<RelaxLoginsDataContract> GetLoginsDataContracts(int iActive)
        {
            var query = from l in _context.RelaxLogins.Where(n => n.active == iActive)
                        select new RelaxLoginsDataContract
                        {
                            Id = Convert.ToInt32(l.id),
                            Active = Convert.ToInt32(l.active),
                            MemberId = Convert.ToInt32(l.memberid ?? 0),
                            UserName = l.username,
                            Password = l.password,
                            Role = Convert.ToInt32(l.role ?? 0)
                        };
            return query;
        }
        #endregion

        #region Add
        public void Add(RelaxLogin login)
        {
            _context.RelaxLogins.InsertOnSubmit(login);
            _context.SubmitChanges();

        }
        #endregion

        #region Hàm Update
        public new void Update(RelaxLogin login)
        {
            var query = _context.RelaxLogins.Where(l => l.id == login.id);
            if (query.Any())
            {
                var firstOrdefault = query.FirstOrDefault();
                if (firstOrdefault != null)
                {
                    firstOrdefault.active = login.active;
                    firstOrdefault.role = login.role;
                    if (!string.IsNullOrEmpty(login.password))
                        firstOrdefault.password = login.password;

                    _context.SubmitChanges();
                }
            }
        }
        #endregion

        #region Hàm Delete
        public void Delete(long sId)
        {
            var query = _context.RelaxLogins.Where(l => l.id == sId);
            if (query.Any())
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                _context.RelaxLogins.DeleteOnSubmit(query.FirstOrDefault());
                _context.SubmitChanges();
            }
        }
        #endregion


    }
}
